const express = require("express");
const router = express.Router();
const pool = require("../pool");

//查询所有用户信息
router.post("/userinfo", (req, res, next) => {
  const size = req.body.size * 1;
  const start = (req.body.nowPage - 1) * size;
  const sql1 = `select * from login where role!=3 limit ${start},${size};`;
  const sql2 = `SELECT ceil(count(username) / ${size}) as pageCount FROM login where role!=3;`;
  const sql3 = `SELECT count(*) as total FROM login where role!=3;`;
  const sql = sql1 + sql2 + sql3;
  pool.query(sql, (err, result) => {
    if (err) return next(err);
    if (result.length == 0) {
      res.send({
        code: 404,
        msg: "未找到信息",
      });
    } else {
      res.send({
        code: 200,
        msg: "success",
        data: {
          result: result[0],
          pageCount: result[1][0].pageCount,
          nowPage: req.body.nowPage,
          total: result[2][0].total,
          size: req.body.size * 1,
        },
      });
    }
  });
});

//模糊查询用户
router.post("/userinfo-single", (req, res, next) => {
  const size = req.body.size * 1;
  const start = (req.body.nowPage - 1) * size;
  const sql1 = req.body.username
    ? `select * from login where role!=3 and username like ? limit ${start},${size};`
    : `select * from login where role!=3 and usertel like ? limit ${start},${size};`;
  const sql2 = req.body.username
    ? `SELECT ceil(count(username) / ${size}) as pageCount FROM login where role!=3 and username like ?;`
    : `SELECT ceil(count(username) / ${size}) as pageCount FROM login where role!=3 and usertel like ?;`;
  const sql3 = req.body.username
    ? `SELECT count(*) as total FROM login where role!=3 and username like ?;`
    : `SELECT count(*) as total FROM login where role!=3 and usertel like ?;`;
  const sql = sql1 + sql2 + sql3;
  let value = `%${req.body.username || req.body.usertel}%`;
  pool.query(sql, [value, value, value], (err, result) => {
    if (err) return next(err);
    if (result.length == 0) {
      res.send({
        code: 404,
        msg: "未找到信息",
      });
    } else {
      res.send({
        code: 200,
        msg: "success",
        data: {
          result: result[0],
          pageCount: result[1][0].pageCount,
          nowPage: req.body.nowPage * 1,
          total: result[2][0].total,
          size: req.body.size * 1,
        },
      });
    }
  });
});

//登录查询用户
router.post("/user", (req, res, next) => {
  // const sql = `select username,password,role from login where ${req.body.username} = ?`;
  pool.query(
    `select username,password,role,usertel from login where username = ?`,
    [req.body.username],
    (err, result) => {
      if (err) return next(err);
      if (result.length == 0) {
        res.send({
          code: 404,
          msg: "未找到信息",
        });
      } else {
        res.send({
          code: 200,
          msg: "success",
          data: result,
        });
      }
    }
  );
});

//管理员信息
router.get("/master", (req, res, next) => {
  pool.query(`select * from login where username = 'master'`, (err, result) => {
    if (err) return next(err);
    if (result.length == 0) {
      res.send({
        code: 404,
        msg: "未找到信息",
      });
    } else {
      res.send({
        code: 200,
        msg: "success",
        data: result,
      });
    }
  });
});

//用户注册
router.post("/adduser", (req, res, next) => {
  // const sql = `INSERT INTO Login VALUES('admin','123456',2,'','','/userimg/15.jpeg','',0);`
  pool.query(
    `INSERT INTO Login VALUES(?,?,?,?,null,'/defaultimg.jpg',null,0);`,
    [req.body.username, req.body.password, req.body.role * 1, req.body.usertel],
    (err, result) => {
      if (err) return next(err);
      res.send({
        code: 200,
        msg: "注册成功",
      });
    }
  );
});

//添加用户
router.post("/adduser-bysys", (req, res, next) => {
  let {
    username,
    password,
    role,
    usertel,
    booklotsnum,
    userimage,
    usercarid,
    balance,
  } = req.body;
  if (!userimage) {
    userimage = "/defaultimg.jpg";
  }
  pool.query(
    `INSERT INTO Login VALUES(?,?,?,?,?,?,?,?);`,
    [
      username,
      password,
      role,
      usertel,
      booklotsnum,
      userimage,
      usercarid,
      balance,
    ],
    (err, result) => {
      if (err) return next(err);
      if (result.affectedRows != 0) {
        res.send({
          code: 200,
          msg: "注册成功",
        });
      } else {
        res.send({
          code: 401,
          msg: "注册失败",
        });
      }
    }
  );
});

//修改用户密码
router.put("/changepwd", (req, res, next) => {
  pool.query(
    `UPDATE login SET password = ? WHERE username = ?`,
    [req.body.userpwd, req.body.username],
    (err, result) => {
      if (err) next(err);
      if (result.changeRows != 0) {
        res.send({
          code: 200,
          msg: "修改成功",
        });
      } else {
        res.send({
          code: 401,
          msg: "修改失败",
        });
      }
    }
  );
});

//修改用户信息
router.post("/update", (req, res, next) => {
  let {
    username,
    password,
    role,
    usertel,
    booklotsnum,
    userimage,
    usercarid,
    balance,
  } = req.body;
  pool.query(
    `UPDATE login SET username=?,password=?,role=?,usertel=?,booklotsnum=?,userimage=?,usercarid=?,balance=? WHERE username = ?`,
    [
      username,
      password,
      role,
      usertel,
      booklotsnum,
      userimage,
      usercarid,
      balance,
      username,
    ],
    (err, result) => {
      if (err) next(err);
      if (result.changeRows != 0) {
        res.send({
          code: 200,
          msg: "修改成功",
        });
      } else {
        res.send({
          code: 401,
          msg: "修改失败",
        });
      }
    }
  );
});

//删除用户
router.post("/delete-user", (req, res, next) => {
  pool.query(
    `delete from login WHERE username = ?`,
    [req.body.username],
    (err, result) => {
      if (err) next(err);
      if (result.affectedRows != 0) {
        res.send({
          code: 200,
          msg: "删除成功",
        });
      } else {
        res.send({
          code: 401,
          msg: "删除失败",
        });
      }
    }
  );
});

module.exports = router;
